/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package funda.appius.arquitectura;

import funda.appius.Avance.Real.AvanceDeObra;
import funda.appius.Avance.Real.Avance_Real;
import funda.appius.Avance.Real.Insumo_Capturado;
import funda.appius.Catalogo.Conceptos.Catalogo_Conceptos;
import funda.appius.Catalogo.Conceptos.Concepto;
import funda.appius.Catalogo.Conceptos.Seccion_Concepto;
import funda.appius.Constructora.Constructora;
import funda.appius.Constructora.Proyecto;
import funda.appius.Constructora.Usuario;
import funda.appius.Presupuesto.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public abstract class AbstractFunda implements IFunda
{

    public String strInsertar,strDelete,strUpdate,strInsertarId,id,tabla;
    private ConexionMySQL conexion = new ConexionMySQL();
    
    protected abstract void fillFunda(ResultSet row);
    protected abstract void Comandos();
    public abstract AbstractFunda get(Object o);
    
    @Override
    public ArrayList<Object> getAtributosInsertar() {
        ArrayList lst = getAtributos();
        lst.remove(lst.size()-1);
        return lst;
    }

    protected AbstractFunda()
    {
        Comandos();
        ModifyStoredProcedures();
    }
        
    @Override
    public void update()
    {
        conexion.AccionesDB(getAtributos(),strUpdate);
    }

    @Override
    public void delete()
    {
        conexion.AccionesDB(getAtributos(),strDelete);
    }

    @Override
    public void insertar()
    {
        conexion.AccionesDB(getAtributosInsertar(),strInsertar);
    }
    
    @Override
    public Object insertarId() {
        return conexion.insertar(getAtributosInsertar(), strInsertar, id, tabla);
    }
    
    public static AbstractFunda getAbstractFunda(AbstractFunda p, Object value, String tabla, String id) 
    {    
        try{
            ResultSet rs = new ConexionMySQL().GetQuery("SELECT * FROM "+tabla+" where "+id+" = '" + value + "'");
            rs.next();
            p.fillFunda(rs);
            return p;
        }
        catch(Exception e){return null;}
    }

    public static List<AbstractFunda> getAll(String tabla, String id, Class clase)
    {
        ResultSet data = new ConexionMySQL().GetQuery("SELECT "+id+" FROM "+tabla);
        List<AbstractFunda> lst = new ArrayList<AbstractFunda>();
        try {
            while(data.next())
                lst.add(AbstractFunda.getAbstractFunda(getNewAbstractAndroid(clase),data.getString(1),tabla,id));
        } catch (SQLException ex) {
            Logger.getLogger(AbstractFunda.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lst;
    }
    
     public static List<AbstractFunda> getAllFrom(String tabla, String id, Object Id, Object Value,Class clase)
    {
        String str = "SELECT "+id+" FROM "+tabla +" WHERE " +Id+ " = " + Value;
        //System.out.println(str);
        ResultSet data = new ConexionMySQL().GetQuery(str);
        List<AbstractFunda> lst = new ArrayList<AbstractFunda>();
        try {
            while(data.next()){
                lst.add(AbstractFunda.getAbstractFunda(getNewAbstractAndroid(clase),data.getString(1),tabla,id));
            }
        } catch (SQLException ex) {
            System.out.println(ex.toString());
        }
        return lst;
    }
    
    public static AbstractFunda getNewAbstractAndroid(Class clase)
    {
        AbstractFunda ret = null;
        if(clase == Constructora.class)
            ret = new Constructora();
        else if (clase == Proyecto.class)
            ret = new Proyecto();
        else if (clase == Usuario.class)
            ret = new Usuario();
        else if (clase == AvanceDeObra.class)
            ret = new AvanceDeObra();
        else if (clase == Avance_Real.class)
            ret = new Avance_Real();
        else if (clase == Insumo_Capturado.class)
            ret = new Insumo_Capturado();
        else if (clase == Catalogo_Conceptos.class)
            ret = new Catalogo_Conceptos();
        else if (clase == Concepto.class)
            ret = new Concepto();
        else if (clase == Seccion_Concepto.class)
            ret = new Seccion_Concepto();
        else if (clase == Indirecto_Catalogo.class)
            ret = new Indirecto_Catalogo();
        else if (clase == Insumo_Catalogo.class)
            ret = new Insumo_Catalogo();
        else if (clase == Listado_Indirectos.class)
            ret = new Listado_Indirectos();
        else if (clase == Listado_Insumos.class)
            ret = new Listado_Insumos();
        else if (clase == Presupuesto.class)
            ret = new Presupuesto();
        else if (clase == Seccion_Indirectos.class)
            ret = new Seccion_Indirectos();
        else if (clase == Seccion_Insumos.class)
            ret = new Seccion_Insumos();
        return ret;
    }

    private void ModifyStoredProcedures() {
        strInsertar = FixProcedure(getAtributos().size()-2,strInsertar);
        strInsertarId = strInsertar;
        strUpdate = FixProcedure(getAtributos().size()-1,strUpdate);
        strDelete = FixProcedure(getAtributos().size()-1,strDelete);
    }
    
    private String FixProcedure(int n, String s)
    {
        s = "{ call " + s + " (";
        for(int x = 0;x<n;x++)
            s+=" ?, ";
        s+= "?) }";
        return s;
    }
}